package com.nny.alarm.more;

import java.util.List;

import com.nny.alarm.MainActivity;
import com.nny.alarm.R;
import com.umeng.analytics.MobclickAgent;
import com.uxwine.alarm.Alarm;
import com.uxwine.alarm.AlarmDao;
import com.uxwine.cmm.BaseFragment;
import com.uxwine.util.UxToast;
import com.uxwine.widget.GestureListView;
import com.uxwine.widget.GestureListView.ScrollType;

import android.content.Context;
import android.database.Cursor;
import android.graphics.EmbossMaskFilter;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;


public class MoreFragment extends BaseFragment implements 
		GestureListView.OnGestureListener, 
		GestureListView.OnItemClickListener, OnClickListener
{
	private View mMainView;
	GestureListView mlvAlarms;
	List<Alarm> mAlarms;
	LayoutInflater mInflater;
//	MAdpater mAdapter;
	MainActivity mAct;
	AlarmBriefUtil mAUtil;
	
	AlarmAdapter mAdapter;
	AlarmDetailView mCurrent;
	Vibrator mVibrator;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mAct = (MainActivity) getActivity();
		mAUtil = AlarmBriefUtil.getInstance(mAct);
		mAdapter = new AlarmAdapter(mAct);
		mVibrator = (Vibrator)mAct.getSystemService(Context.VIBRATOR_SERVICE);
	}
	
	

	@Override
	public void onDestroy() {
		mAdapter.recycle();
		super.onDestroy();
	}



	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		mMainView = inflater.inflate(R.layout.fragment_more, container, false);
		mlvAlarms = (GestureListView) mMainView.findViewById(R.id.main_lv_alarmlist);
		View empty = mMainView.findViewById(R.id.more_ctn_emtpy);
		mMainView.findViewById(R.id.more_btn_add).setOnClickListener(this);
		
		mlvAlarms.setOnGestureListener(this);
		mlvAlarms.setOnItemClickedListener(this);
		mlvAlarms.setAdapter(mAdapter);		
		mlvAlarms.setEmptyView(empty);
		return mMainView;
	}

	public boolean loadAlarms() 
	{
		boolean bHasAlarm = false;
		mAdapter.clear();
		mAdapter.notifyDataSetInvalidated();
		Cursor cursor = AlarmDao.getAlarmsCursor(mAct.getContentResolver());
		if(null != cursor)
		{
			while(cursor.moveToNext())
			{
				mAdapter.add(new AlarmDetailView(mAct, new Alarm(cursor), mAUtil), false);
				bHasAlarm = true;
			}
			cursor.close();
		}
		mAdapter.notifyDataSetChanged();
		return bHasAlarm;
	}

	private void editAlarm(Alarm a)
	{
		mAct.showEidtor(a);
		if(null != a)
			UxToast.sshow(mAct, R.string.edit_start_edit);
	}

	@Override
	public void onDrop(ScrollType type, boolean bOverred, int position) {
		if(null == mCurrent)
			return;
		if(!bOverred)
		{
			mCurrent.showDetail();
			return;
		}
		switch(type)
		{
		case LEFT:
			mAdapter.enableAlarm(position, !mCurrent.mAlarm.enabled, true);
			MobclickAgent.onEvent(getActivity(), "more_op", "enable:" + mCurrent.mAlarm.enabled);
			break;
		case RIGHT:
			mAdapter.deleteAlarm(position, true);
			MobclickAgent.onEvent(getActivity(), "more_op", "delete");
			break;
		}
	}



	@Override
	public void onDragged(int position) {
		mCurrent = (AlarmDetailView)mAdapter.getItem(position);
	}

	@Override
	public void onItemClicked(int position)
	{
		AlarmDetailView adv = (AlarmDetailView)mAdapter.getItem(position);
		if(null != adv)
			editAlarm(adv.mAlarm);		
	}

	@Override
	public void onScroll(ScrollType type, float percent) {
		if(null == mCurrent)
			return;
		switch(type)
		{
		case LEFT:
			mCurrent.showDisable(true);
			break;
		case RIGHT:
			mCurrent.showDelete(true);
			break;
		case NONE:
			mCurrent.showDetail();
			break;
		}
		
	}

	@Override
	public void onOver(ScrollType type, boolean bOverred) {
		if(null == mCurrent)
			return;
		switch(type)
		{
		case LEFT:
			mCurrent.showDisable(!bOverred);
			break;
		case RIGHT:
			if(bOverred)
				mVibrator.vibrate(50);
			mCurrent.showDelete(!bOverred);
			break;
		}
		
	}



	@Override
	public void onClick(View v) {
		editAlarm(null);
	}
}
